﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using badminton;

namespace badminton.Controls
{
    public partial class LoginCtrl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                /////////
                this.DropDownList_usertype.Items.Add(new ListItem("系统管理员"));
                this.DropDownList_usertype.Items.Add(new ListItem("球馆管理员"));
                this.DropDownList_usertype.Items.Add(new ListItem("教练"));
                this.DropDownList_usertype.Items.Add(new ListItem("学员"));

                Literal1.Text = GetWelcomeText();
                Literal2.Text = GetLinkText();
            }
        }

        protected void OnLogin(object sender, EventArgs e)
        {
            string usertype = this.DropDownList_usertype.SelectedItem.Text;
            string logname = this.UserName.Text.Trim();
            string pass = this.Password1.Text.Trim();
            int userId = -1;

            this.Password1.Text = "";

            if(logname == "")
            {
                this.FailureText.Text = "请输入登录名";
                return;
            }

            BadmintonEntities bde = new BadmintonEntities();
            if (usertype == "系统管理员")
            {
                SysAdmin sa = bde.SysAdmin.FirstOrDefault(p => p.name == logname || p.userName == logname);
                if (sa == null)
                {
                    this.FailureText.Text = "没有找到该用户。";
                    return;
                }
                if (sa.password.Trim() != pass)
                {
                    this.FailureText.Text = "密码不正确。";
                    return;
                }

                userId = sa.id;
            }
            else if (usertype == "球馆管理员")
            {
                HallAdmin sa = bde.HallAdmin.FirstOrDefault(p => p.name == logname || p.userName == logname);
                if (sa == null)
                {
                    this.FailureText.Text = "没有找到该用户。";
                    return;
                }
                if (sa.password.Trim() != pass)
                {
                    this.FailureText.Text = "密码不正确。";
                    return;
                }

                userId = sa.id;
            }
            else if (usertype == "教练")
            {
                Teacher sa = bde.Teacher.FirstOrDefault(p => p.name == logname || p.userName == logname);
                if (sa == null)
                {
                    this.FailureText.Text = "没有找到该用户。";
                    return;
                }
                if (sa.password.Trim() != pass)
                {
                    this.FailureText.Text = "密码不正确。";
                    return;
                }

                userId = sa.id;
            }
            else if (usertype == "学员")
            {
                Student sa = bde.Student.FirstOrDefault(p => p.name == logname || p.userName == logname);
                if (sa == null)
                {
                    this.FailureText.Text = "没有找到该用户。";
                    return;
                }
                if (sa.password.Trim() != pass)
                {
                    this.FailureText.Text = "密码不正确。";
                    return;
                }

                userId = sa.id;
            }

            if (userId==-1)
            {
                    this.FailureText.Text = "登陆失败。";
                    return;
            }

            LoginSession login = new LoginSession();
            login.m_loginType = usertype;
            login.m_loginId = userId;
            
            Session["Login"] = login;

            Literal1.Text = GetWelcomeText();
            Literal2.Text = GetLinkText();

        }

        public string GetWelcomeText()
        {
            if (Session["Login"]==null)
            {
                return "";
            }
            LoginSession login = Session["Login"] as LoginSession;

            BadmintonEntities bde = new BadmintonEntities();
            if (login.m_loginType=="系统管理员")
            {
                SysAdmin sa = bde.SysAdmin.FirstOrDefault(p => p.id == login.m_loginId);
                if (sa==null)
                {
                    return "";
                }

                return "欢迎" + login.m_loginType + "<font color='blue'>" + sa.name + "</font>, 请";
            }
            else if (login.m_loginType=="球馆管理员")
            {
                HallAdmin sa = bde.HallAdmin.FirstOrDefault(p => p.id == login.m_loginId);
                if (sa == null)
                {
                    return "";
                }

                return "欢迎" + login.m_loginType + "<font color='blue'>" + sa.name + "</font>, 你可以：";
            }
            else if (login.m_loginType=="教练")
            {
                Teacher sa = bde.Teacher.FirstOrDefault(p => p.id == login.m_loginId);
                if (sa == null)
                {
                    return "";
                }

                return "欢迎" + login.m_loginType + "<font color='blue'>" + sa.name + "</font>, 你可以：";
            }
            else if (login.m_loginType=="学员")
            {
                Student sa = bde.Student.FirstOrDefault(p => p.id == login.m_loginId);
                if (sa == null)
                {
                    return "";
                }

                return "欢迎" + login.m_loginType + "<font color='blue'>" + sa.name + "</font>, 你可以：";
            }

            return "";
        }

        public string GetLinkText()
        {
            if (Session["Login"] == null)
            {
                return "";
            }

            LoginSession login = Session["Login"] as LoginSession;
            if (login.m_loginType == "学员")
            {
                return "<a href='./pages/MyTraining.aspx'>" + "进入我的培训" +"</a>";
            }
            else
            {
                return "<a href='./admin.aspx'>" + "进入后台管理" + "</a>";
            }

        }
    }
}